package com.example.idlefish.GoodsInfo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.baidu.location.BDLocation
import com.baidu.location.BDLocationListener
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.common.BaiduMapSDKException
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.BitmapDescriptor
import com.baidu.mapapi.map.BitmapDescriptorFactory
import com.baidu.mapapi.map.MapPoi
import com.baidu.mapapi.map.MapStatusUpdate
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MarkerOptions
import com.baidu.mapapi.map.OverlayOptions
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.search.core.SearchResult
import com.baidu.mapapi.search.geocode.GeoCodeResult
import com.baidu.mapapi.search.geocode.GeoCoder
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult
import com.example.idlefish.R
import com.example.idlefish.databinding.MainActivityBinding
import com.example.idlefish.databinding.MaplayoutBinding

class MapActivity : AppCompatActivity() {
    private lateinit var binding:MaplayoutBinding
    private lateinit var localClient: LocationClient
    private var nowLatLng: LatLng? = null
    private lateinit var geoCoder: GeoCoder
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LocationClient.setAgreePrivacy(true)
        SDKInitializer.setAgreePrivacy(applicationContext, true)
        try {
            SDKInitializer.initialize(applicationContext)
            SDKInitializer.setCoordType(CoordType.GCJ02)
        }catch (e:BaiduMapSDKException){

        }
        binding = MaplayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initLocationClient()
        binding.mapView.map.setOnMapClickListener(object : BaiduMap.OnMapClickListener {
            override fun onMapClick(point: LatLng) {
                // 用户点击地图上的点
                val dot: BitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.location_point)

                val option: OverlayOptions = MarkerOptions()
                    .position(point)
                    .icon(dot)
                binding.mapView.map.addOverlay(option)
                // 反向地理编码
                geoCoder.reverseGeoCode(ReverseGeoCodeOption().location(point))
            }

            override fun onMapPoiClick(poi: MapPoi?) {
                val point = poi?.position
                val dot: BitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.location_point)

                val option: OverlayOptions = MarkerOptions()
                    .position(point)
                    .icon(dot)
                binding.mapView.map.addOverlay(option)
                // 反向地理编码
                geoCoder.reverseGeoCode(ReverseGeoCodeOption().location(point))
            }

        })
        binding.close.setOnClickListener {
            val intent = Intent()
            intent.putExtra("address", binding.mapresult.text)
            setResult(RESULT_OK, intent)
            finish()
        }
        geoCoder = GeoCoder.newInstance()
        geoCoder.setOnGetGeoCodeResultListener(object : OnGetGeoCoderResultListener {
            override fun onGetGeoCodeResult(result: GeoCodeResult?) {

            }

            override fun onGetReverseGeoCodeResult(result: ReverseGeoCodeResult?) {
                result?.let{
                    if(it.error == SearchResult.ERRORNO.NO_ERROR){
                        val address = it.address

                            binding.mapresult.text = "$address"
                            val intent = Intent()
                            intent.putExtra("address", binding.mapresult.text)
                            setResult(RESULT_OK, intent)
                            finish()

                    }
                    else{

                            binding.mapresult.text = "获取位置信息失败:${it.error}"

                    }
                }
            }
        })
    }
    override fun onDestroy() {
        super.onDestroy()
        localClient.stop()
        binding.mapView.map.isMyLocationEnabled = false
        binding.mapView.onDestroy()
    }

    override fun onPause() {
        super.onPause()

        binding.mapView.onPause()
    }

    override fun onResume() {
        super.onResume()
        binding.mapView.onResume()
    }
    private fun initLocationClient(){
        localClient = LocationClient(applicationContext)
        val option = LocationClientOption()
        option.openGps = true
        option.setCoorType("GCJ02")
        //设置扫描间隔
        option.setScanSpan(3000)
        localClient.locOption = option
        //生成一个监听器
        localClient.registerLocationListener(MyLocationListener())
        localClient.start()
    }
    private  inner  class MyLocationListener: BDLocationListener {
        override fun onReceiveLocation(location: BDLocation?) {
            val latLng = location?.let { LatLng(it.latitude, location.longitude) }
            nowLatLng = latLng
            val zoom = 18.0f
            val update = MapStatusUpdateFactory.newLatLngZoom(nowLatLng, zoom)
            binding.mapView.map.animateMapStatus(update)
            val dot: BitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.img_1)

            val option: OverlayOptions = MarkerOptions()
                .position(nowLatLng)
                .icon(dot)
            binding.mapView.map.addOverlay(option)
            //不能放到oncreate函数里 没有初始化好不能直接调用
        }
    }
}